//429. N 叉树的层序遍历

import java.util.*;
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}
class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root==null){
            return ans;
        }
        Queue<Node> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size=q.size();
            List<Integer> temp=new ArrayList<>();
            for(int i=0;i<size;i++){

                Node tmp=q.poll();
                temp.add(tmp.val);
                for(int j=0;j<tmp.children.size();j++){
                    if(tmp.children.get(j)!=null){
                        q.offer(tmp.children.get(j));
                    }
                }
            }
            ans.add(temp);
        }
        return ans;
    }
}